package logic

import (
	"context"

	"monitor/rpc/status/internal/svc"
	"monitor/rpc/status/status"

	"github.com/tal-tech/go-zero/core/logx"
	//手动代码
	"database/sql"
	"fmt"
	"monitor/rpc/status/model"
	"monitor/shared"
	"time"
)

type StatusUploadLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStatusUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StatusUploadLogic {
	return &StatusUploadLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StatusUploadLogic) StatusUpload(in *status.StatusUploadReq) (*status.StatusUploadResp, error) {
	// todo: add your logic here and delete this line

	//检查 缓存中是否有值
	var cmd int32 = 0
	err := l.svcCtx.Cache.Get(shared.CacheSnPrefix+in.Sn, &cmd)
	if err != nil && err == shared.ErrNotFound {
		fmt.Println(err)
	} else {
		fmt.Println("GetCache ok:", cmd)
		l.svcCtx.Cache.Del(shared.CacheSnPrefix + in.Sn)
	}
	// 手动代码开始，插入记录到数据库
	t, _ := time.Parse("2006-01-02", in.Ndate)
	_, err = l.svcCtx.Model.Insert(model.TbStatus{
		Sn:     sql.NullString{in.Sn, true},
		Posno:  sql.NullString{in.Posno, true},
		City:   sql.NullString{in.City, true},
		Tyid:   sql.NullString{in.Tyid, true},
		Sver:   sql.NullString{in.Sver, true},
		Carno:  sql.NullString{in.Carno, true},
		Jd:     sql.NullString{in.Jd, true},
		Wd:     sql.NullString{in.Wd, true},
		Unum1:  sql.NullInt64{int64(in.Unum1), true},
		Unum2:  sql.NullInt64{int64(in.Unum2), true},
		Ndate:  sql.NullTime{t, true},
		Ntime:  sql.NullString{in.Ntime, true},
		Amount: sql.NullInt64{int64(in.Amount), true},
		Count:  sql.NullInt64{int64(in.Count), true},
		Line:   sql.NullInt64{int64(in.Line), true},
		Stime:  sql.NullString{in.Stime, true},
		Ctime:  sql.NullString{in.Ctime, true},
		Empid:  sql.NullString{in.Empid, true},
		Rvs1:   sql.NullString{in.Rvs1, true},
		Rvs2:   sql.NullString{in.Rvs2, true},
		Pver:   sql.NullInt64{int64(in.Pver), true},
		Bver:   sql.NullInt64{int64(in.Bver), true},
		Tenant: sql.NullInt64{int64(in.Tenant), true},
		Alarm:  sql.NullInt64{int64(in.Alarm), true},
		Cmd:    sql.NullInt64{int64(cmd), true},
	})
	if err != nil {
		return nil, err
	}
	if in.Alarm != 0 {
		//设备报警，存储报警记录
		info := ""
		_, err = l.svcCtx.ModelAlarm.Insert(model.TbAlarm{
			Sn:       sql.NullString{in.Sn, true},
			Posno:    sql.NullString{in.Posno, true},
			City:     sql.NullString{in.City, true},
			Tyid:     sql.NullString{in.Tyid, true},
			Sver:     sql.NullString{in.Sver, true},
			Carno:    sql.NullString{in.Carno, true},
			Jd:       sql.NullString{in.Jd, true},
			Wd:       sql.NullString{in.Wd, true},
			Unum1:    sql.NullInt64{int64(in.Unum1), true},
			Unum2:    sql.NullInt64{int64(in.Unum1), true},
			Datetime: sql.NullString{time.Now().Format("2006-01-02 15:04:05"), true},
			Amount:   sql.NullInt64{int64(in.Amount), true},
			Count:    sql.NullInt64{int64(in.Count), true},
			Line:     sql.NullInt64{int64(in.Line), true},
			Stime:    sql.NullString{in.Stime, true},
			Ctime:    sql.NullString{in.Ctime, true},
			Empid:    sql.NullString{in.Empid, true},
			Rvs1:     sql.NullString{in.Rvs1, true},
			Rvs2:     sql.NullString{in.Rvs2, true},
			Pver:     sql.NullInt64{int64(in.Pver), true},
			Bver:     sql.NullInt64{int64(in.Bver), true},
			Tenant:   sql.NullInt64{int64(in.Tenant), true},
			Alarm:    sql.NullInt64{int64(in.Alarm), true},
			Info:     sql.NullString{info, true},
		})
		if err != nil {
			return nil, err
		}
	}

	return &status.StatusUploadResp{Code: 0, Msg: "server resp,save record ok", Cmd: cmd}, nil
}
